<template>
	<view>
		<view class="wrap">
			<view class="u-search-box">
				<u-search placeholder="输入商品搜索" class="u-search-text" v-model="keyword" @custom="searchGoods" @clear="clearSearch"></u-search>
			</view>
			<view class="u-text-center u-m-t-30">
				<u-tabs :list="list" bar-width="100" item-width="150" :is-scroll="false" :current="currentSort" @change="changeSort"></u-tabs>
			</view>
			<view class="page-box">
				<view class="order" v-for="(item, index) in orderList" :key="item.id" @tap="orderInfo(item.id)">
					<view class="top">
						<view class="left">
							<u-icon name="home" :size="30" color="rgb(94,94,94)"></u-icon>
							<view class="store">{{ item.order_no }}</view>
							<u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon>
						</view>
						<view class="right">{{ status[item.status-1] }}</view>
					</view>
					<view class="item"  v-for="(goods, gindex) in item.goods.data" :key="gindex">
						<view class="left"><image :src="goods.cover_url" mode="aspectFill"></image></view>
						<view class="content">
							<view class="title u-line-2">{{ goods.title }}</view>
							<view class="type">{{ goods.description }}</view>
							<view class="delivery-time">订单：{{ goods.updated_at }}</view>
						</view>
						
							<view class="right">
							<view class="price">
								￥{{ priceInt(item.orderDetails.data[gindex].price) }}
								<text class="decimal">.{{ priceDecimal(item.orderDetails.data[gindex].price) }}</text>
							</view>
							<view class="number">x{{ item.orderDetails.data[gindex].num }}</view>
						</view>
					</view>
					<view class="total">
						共{{ totalNum(item.orderDetails.data) }}件商品 合计:
						<text class="total-price">
							<!-- ￥{{ priceInt(totalPrice(item.orderDetails.data)) }}.
							<text class="decimal">{{ priceDecimal(totalPrice(item.orderDetails.data)) }}</text> -->
							{{item.amount}}
						</text>
					</view>
					<view class="bottom">
						<view class="more"><u-icon name="more-dot-fill" color="rgb(203,203,203)"></u-icon></view>
						<view class="logistics btn">查看物流</view>
						<view class="exchange btn">卖了换钱</view>
						<view class="evaluate btn">{{ pay[item.status-1] }}</view>
					</view>
				</view>
				<u-loadmore :status="loadStatus[0]" bgColor="#f2f2f2"></u-loadmore>
			</view>
			
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			orderList: [[], [], [], [], []],
			list: [
				{
					name: '全部'
				},
				{
					name: '待付款'
				},
				{
					name: '已支付'
				},
				{
					name: '待收货'
				},
				{
					name: '已完成',
					count: 0
				},
				{
					name: '已过期',
					count: 0
				}
			],
			status:[
				'未支付','已付款','已发货','已完成','已过期'  //  去付款 ， '' ,  确认收货 ， 立即评价 ， ’‘
			], //订单状态
			pay:[
				'去付款','','确认收货','立即评价',''  //  去付款 ， '' ,  确认收货 ， 立即评价 ， ’‘
			], //订单状态
			// current: 0,
			// swiperCurrent: 0,
			// tabsHeight: 0,
			// dx: 0,
			loadStatus: ['loadmore','loadmore','loadmore','loadmore','loadmore'],
			
			orderStatus:0, //订单状态: 1下单 2支付 3发货 4收货 5过期
			currentSort: 0,
			page: 1, //分页
			keyword: '', //标题模糊搜索
			total_pages: 1 //总页码
		};
	},
	onLoad() {
		this.getOrderList();
	},
	computed: {
		// 价格小数
		priceDecimal() {
			return val => {
				if(this.$u.test.amount(val) ){
					if (val !== parseInt(val)) return val.slice(-2);
				    else return '00';
				}
				return false;
			};
		},
		// 价格整数
		priceInt() {
			return val => {
				if(this.$u.test.amount(val) ){
					if (val !== parseInt(val)) return val.split('.')[0];
				    else return val;
				}else{
					return val;
				}
			};
		}
	},
	methods: {
		// 根据排序重置商品数据
		changeSort(index) {
			this.orderStatus = index;
			this.currentSort = index;
			// 重置商品数据和分页
			this.initData();
		},
		// 页面数据
		async getOrderList() {
			const param = {
				page: this.page,
				title: this.keyword,
				// status:idx,
				include:'goods,user,orderDetails'
			}
			// 判断 订单状态
			if (this.orderStatus) param.status = this.orderStatus;
			const res = await this.$u.api.orderList(param);
			// console.log(res);
			// this.orderList = res.data;
			this.orderList = this.orderList[0].id ? [...this.orderList,...res.data]:res.data;
			this.total_pages = res.meta.pagination.total_pages;
			this.list[this.currentSort].count = res.meta.pagination.total;

		
		},
		/**
		 * 搜索商品
		 */
		searchGoods() {
			// 搜索商品，进行重置分类ID
			this.current = 0;
			this.getOrderList();
		},
		/**
		 * 清除搜索商品
		 */
		clearSearch() {
			this.keyword = '';
			this.initData();
		},
		/**
		 * 重置商品和分页
		 */
		initData() {
			this.page = 1;
			this.orderList = [{},{},{},{}] ;
			this.getOrderList();
		},
		// 页面滚动到底部的事件
		onReachBottom() {
			console.log(this.page );
			console.log(this.total_pages);
			// 如果是最后一页，则返回
			if(this.page >= this.total_pages) return ;
			// 重新请求数据，带上分页参数
			this.page = this.page + 1 ;
			this.getOrderList();
		},
		// 总价
		totalPrice(item) {
			let price = 0;
			item.map(val => {
				price += parseFloat(val.price*val.num);
			});
			return price.toFixed(2);
		},
		// 总件数
		totalNum(item) {
			let num = 0;
			item.map(val => {
				num += val.num;
			});
			return num;
		},
		// tab栏切换
		change(index) {
			this.swiperCurrent = index;
			this.getOrderList(index);
		},
		// 订单详情
		orderInfo(id) {
			this.$u.route({
				url: 'pages/order/orderInfo',
				params: {
					order: id
				}
			})
		},
	}
};
</script>

<style>
/* #ifndef H5 */
page {
	height: 100%;
	background-color: #f2f2f2;
}
/* #endif */
</style>

<style lang="scss" scoped>
	.u-search-box {
		padding: 18rpx 30rpx;
		background-color: #FFFFFF;
	}

.order {
	width: 710rpx;
	background-color: #ffffff;
	margin: 20rpx auto;
	border-radius: 20rpx;
	box-sizing: border-box;
	padding: 20rpx;
	font-size: 28rpx;
	.top {
		display: flex;
		justify-content: space-between;
		.left {
			display: flex;
			align-items: center;
			.store {
				margin: 0 10rpx;
				font-size: 32rpx;
				font-weight: bold;
			}
		}
		.right {
			color: $u-type-warning-dark;
		}
	}
	.item {
		display: flex;
		margin: 20rpx 0 0;
		.left {
			margin-right: 20rpx;
			image {
				width: 200rpx;
				height: 200rpx;
				border-radius: 10rpx;
			}
		}
		.content {
			.title {
				font-size: 28rpx;
				line-height: 50rpx;
			}
			.type {
				margin: 10rpx 0;
				font-size: 24rpx;
				color: $u-tips-color;
			}
			.delivery-time {
				color: #e5d001;
				font-size: 24rpx;
			}
		}
		.right {
			margin-left: 10rpx;
			padding-top: 20rpx;
			text-align: right;
			.decimal {
				font-size: 24rpx;
				margin-top: 4rpx;
			}
			.number {
				color: $u-tips-color;
				font-size: 24rpx;
			}
		}
	}
	.total {
		margin-top: 20rpx;
		text-align: right;
		font-size: 24rpx;
		.total-price {
			font-size: 32rpx;
		}
	}
	.bottom {
		display: flex;
		margin-top: 40rpx;
		padding: 0 10rpx;
		justify-content: space-between;
		align-items: center;
		.btn {
			line-height: 52rpx;
			width: 160rpx;
			border-radius: 26rpx;
			border: 2rpx solid $u-border-color;
			font-size: 26rpx;
			text-align: center;
			color: $u-type-info-dark;
		}
		.evaluate {
			color: $u-type-warning-dark;
			border-color: $u-type-warning-dark;
		}
	}
}
.centre {
	text-align: center;
	margin: 200rpx auto;
	font-size: 32rpx;
	image {
		width: 164rpx;
		height: 164rpx;
		border-radius: 50%;
		margin-bottom: 20rpx;
	}
	.tips {
		font-size: 24rpx;
		color: #999999;
		margin-top: 20rpx;
	}
	.btn {
		margin: 80rpx auto;
		width: 200rpx;
		border-radius: 32rpx;
		line-height: 64rpx;
		color: #ffffff;
		font-size: 26rpx;
		background: linear-gradient(270deg, rgba(249, 116, 90, 1) 0%, rgba(255, 158, 1, 1) 100%);
	}
}
.wrap {
	display: flex;
	flex-direction: column;
	height: calc(100vh - var(--window-top));
	width: 100%;
}
// .swiper-box {
// 	flex: 1;
// }
// .swiper-item {
// 	height: 100%;
// }
</style>
